﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Infrastructure.Controls;
using Infrastructure.HardCoding;
using LinQProject;
using ServiceProject;
using ServiceProject.EntityService;
using ServiceProject.VirtualClasses;

public partial class ucProduct : UserControl
{

	#region Fields (4) 

    private static readonly FarmacieDataClassesDataContext database = new FarmacieDataClassesDataContext();
  //  protected MaterialColourService matColourService = new MaterialColourService(database);
    protected ProductBussiness prodBussiness = new ProductBussiness(new ProductService(database));
    protected ProductService prodService = new ProductService(database);
 //  protected MaintananceBussiness maintBussiness=new MaintananceBussiness(database);
	#endregion Fields 

	#region Properties (4) 

    public int Index
    {
        get
        {
            int intValue;
            if (Int32.TryParse(hdnIndex.Value, out intValue))
            {
                return intValue;
            }
            return -1;
        }
        set { hdnIndex.Value = value.ToString(); } 
    }

    public bool IsCos
    {
        get
        {
            if (hdnIsCos.Value != null)
                return Convert.ToBoolean(hdnIsCos.Value);
            return false;
        }
        set
        {
            hdnIsCos.Value = value.ToString();
        }
    }

    public bool IsForModify
    {
        get
        {
            if (hdnIsForModify.Value != null)
                return Convert.ToBoolean(hdnIsForModify.Value);
            return false;
        }
        set
        {
            hdnIsForModify.Value = value.ToString();
            //ddlColour.Enabled = value;
            //ddlMaterial.Enabled = value;
            btnSave.Visible = value;
        }
    }

    public int ProductId
    {
        get
        {
            int intValue;
            if (Int32.TryParse(hdnProductId.Value, out intValue))
            {
                return intValue;
            }
            return -1;
        }
        set { hdnProductId.Value = value.ToString(); }
    }
    
    public int MaterialId
    {
        get
        {
            int intValue;
            if (Int32.TryParse(hdnMaterialId.Value, out intValue))
            {
                return intValue;
            }
            return -1;
        }
        set
        {
            hdnMaterialId.Value = value.ToString();
        }
    }

    public bool IsDiscountProduct
    {
        get
        {
            bool result;
            if (hdnIsDiscountProduct.Value != null && Boolean.TryParse(hdnIsDiscountProduct.Value,out result))
                return result;
            //if (Convert.ToInt32(maintBussiness.GetSetting((int)Enums.settings.productOfDayId).SetariValue) == ProductId)
            //    return true;
           return false;
        }
        set
        {
            hdnIsDiscountProduct.Value = value.ToString();
        }
    }

	#endregion Properties 

	#region Methods (12) 


	// Public Methods (2) 

    public void LoadObjects()
    {
        //ddlColour.Enabled = false;
        txtPrice.Enabled = false;

        LoadChangeableDetails();
        //LoadDdlMaterial(ProductId);
    }

    //public void SetImageColour(int? materialColourId)
    //{
    //    if (materialColourId == null)
    //        //img.ImageUrl = Const.NoPhoto;
    //    else
    //    {
    //        //MaterialColour mc = matColourService.GetMaterialColour((int)materialColourId);
    //        //if (mc != null)
    //        //    img.ImageUrl = mc.MaterialColourPhotoPath;
    //    }
    //}

    // Protected Methods (4) 

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int result;
        if (!Int32.TryParse(txtCant.Text,out result))
        {
            Display.DisplayMessages(lblError,Message.noQuantity,true);
            return;
        }
        if (result == 0)
        {
            Display.DisplayMessages(lblError, Message.noQuantity, true);
            return;
        }
        
        Display.DisplayMessages(lblError,string.Empty, false);

        //string materialId = ddlMaterial.SelectedValue;
        //string materialColourS = ddlColour.SelectedValue;
       // string materialColour = String.IsNullOrEmpty(materialColourS) ? "null" : materialColourS;

        string keyToFind = string.Format("{0}", ProductId);
        IDictionary<string, SelectedProduct> Selection = IsCos
                                    ? (IDictionary<string, SelectedProduct>) Session["SelectionForCos"]
                                    : (IDictionary<string, SelectedProduct>) Session["SelectionForRequest"];
        if (Selection.ContainsKey(keyToFind))
            Selection[keyToFind].ProductQuantity = Convert.ToInt32(txtCant.Text);
        OnSave(sender,e);
    }

    //protected void ddlColour_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    int materialColourId = Convert.ToInt32(ddlColour.SelectedValue);
    //    SetImageColour(materialColourId);
    //}

    //protected void ddlMaterial_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    if (ddlMaterial.SelectedValue == "Toate")
    //        return;

    //    int materialId = Convert.ToInt32(ddlMaterial.SelectedValue);
    //    DisplayPriceForMaterialId(materialId.ToString());
    //    LoadDllColour(materialId);

    //}

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadObjects();
    }

    // Private Methods (6) 

    private void DisplayPriceForMaterialId(string value)
    {
        const bool IsDiscountProd = false;
        if (ProductId > 0)
        {
            //decimal productPrice = prodBussiness.GetPriceForProductAndMaterial(ProductId, Convert.ToInt32(value),
            //                                                                   IsDiscountProd);
            decimal productPrice = 0;
            txtPrice.Text = productPrice.ToString();
        }
    }

    private void LoadChangeableDetails()
    {
        Product selectedProduct = prodBussiness.GetProduct(ProductId);

        if (selectedProduct == null)
            return;

        lblName.Text = selectedProduct.ProductName;
        txtPrice.Text = selectedProduct.FinalPrice.ToString();
        //if (selectedProduct.isAvailable)
        //{
            txtInfo.Text = "Disponibil";
            txtInfo.CssClass = "labelAvailable";
        //}
        //else
        //{
        //    txtInfo.Text = "Indisponibil";
        //    txtInfo.CssClass = "labelNotAvailable";
        //}
        //lblDiscount.Visible = IsDiscountProduct;
        //lblDiscount.Text =IsDiscountProduct ? "Discount ": string.Empty;
        //+maintBussiness.GetSetting((int)Enums.settings.discountProduct).SetariValue+"%"
        LoadindDiv(selectedProduct);
        LoadImages(selectedProduct);
    }

    //private void LoadDdlMaterial(int productId)
    //{
    //    //IList<Material> listMaterial = prodBussiness.GetAllMaterialForProduct(productId);
    //    //BindEntityToControls<Material>.BindEntityListToDDL(ddlMaterial, listMaterial, "MaterialName", "MaterialId", null);
    //    ddlMaterial.DataBind();
    //    if (MaterialId == -1)
    //        ddlMaterial.SelectedIndex = 0;
    //    else
    //        ddlMaterial.SelectedValue = MaterialId.ToString();

    //    DisplayPriceForMaterialId(ddlMaterial.SelectedValue);
    //    int result;
    //    if (!Int32.TryParse(ddlMaterial.SelectedValue, out result))
    //       result = -1;
    //    LoadDllColour(result);
    //}

    private void LoadDllColour(int materialId)
    {
        if (materialId < 0)
            return;
        //IList<MaterialColour> list = matColourService.GetColoursForMaterial(materialId);
        //if (list.Count == 0)
        //{
        //    ddlColour.Items.Clear();
        //    ddlColour.Enabled = false;
        //    SetImageColour(null);
        //}
        //else
        //{
        //    BindEntityToControls<MaterialColour>.BindEntityListToDDL(ddlColour, list, "MaterialColourName",
        //                                                             "MaterialColourId", null);
        //    ddlColour.DataBind();
        //    ddlColour.Enabled = true;
        //    ddlColour.SelectedIndex = 0;
        //    SetImageColour(Convert.ToInt32(ddlColour.SelectedValue));
        //}
    }

    private void LoadImages(Product selectedProduct)
    {

        imgPhoto.Src = "~/" + selectedProduct.ProductPicturePath;
        imgPhoto.Alt = Const.ScauneIeftine + selectedProduct.ProductName + Const.Photo;

        txtDescription.Text = selectedProduct.ProductDescription.Substring(0,100)+"...";
        //txtVolume.Text = string.Empty;
        //txtWeight.Text = string.Empty;

        //imgDim.Src = selectedProduct.ProductDimenssion.ProductDimensssionImage;
        //imgDim.Alt = Const.ScauneIeftine + selectedProduct.ProductName + Const.Photo + Const.Dimmenssion;

    }

    private void LoadindDiv(Product selectedProduct)
    {
        HtmlGenericControl divImage = new HtmlGenericControl("div");
        HtmlGenericControl divImageDim = new HtmlGenericControl("div");
        if (ProductId > 0)
        {
            divImage.ID = "mydiv" + ProductId;
            divImage.Style.Add("display", "none");
            Controls.Add(divImage);
            aFacebook.HRef = "#" + divImage.ClientID;

            Image imageHidden = new Image
                                    {
                                        ID = "imgHidden",
                                        ImageUrl = ("~/" + selectedProduct.ProductPicturePath)
                                    };
            divImage.Controls.Add(imageHidden);

            //----------------------------------------------------
            //divImageDim.ID = "mydivDim" + ProductId;
            //divImageDim.Style.Add("display", "none");
            //Controls.Add(divImageDim);
            //aDimFacebook.HRef = "#" + divImageDim.ClientID;
            //Image imageDimHidden = new Image
            //                           {
            //                               ID = "imgHiddenDim",
            //                               ImageUrl = selectedProduct.ProductDimenssion.ProductDimensssionImage
            //                           };
            //divImageDim.Controls.Add(imageDimHidden);
        }

    }

    #endregion Methods 
    
    #region Delegates && Events
    public delegate void SaveEventHandler(object sender, EventArgs e);
    public event SaveEventHandler Save;

    protected void OnSave(object sender, EventArgs e)
    {
        if (Save != null)
        {
            Save(sender, e);
        }
    }
    #   endregion
}
